package office.action.sale;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import current.security.user.domain.User;

import office.action.sale.form.CustomerConditionForm;
import office.action.sale.form.CustomerTrackingForm;
import office.action.sale.wrapper.CustomerWrapper;
import office.bo.sale.impl.Customer;
import office.bo.sale.impl.CustomerTracking;
import office.bo.sale.impl.TrackingInfo;
import office.service.sale.ICustomerService;
import office.service.sale.ITrackingService;
import platform.common.action.BaseAction;
import platform.util.DateUtil;

@Namespace("/customerTracking")
@ParentPackage("struts-base")
@Controller("customerTrackingAction")
@Scope("prototype")
public class CustomerTrackingAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	@Resource
	private ITrackingService trackingService;
	private ICustomerService customerService; //客户Service
	private List<CustomerTracking> ctList;
	private List<TrackingInfo> tList;
	private List<CustomerWrapper> customerWrapperList;
	private CustomerTrackingForm trackingForm;
	private CustomerConditionForm customerCondition;//客户查询条件对象
	@Action(value="gotoCustomerTrackingMenu",results={@Result(location="/pages/sale/customerTrackingXml.jsp")})
	public String gotoCustomerTrackingMenu(){
		return "success";
	}

	@Action(value="gotoEditCustomerTracking",results={@Result(location="/pages/sale/customerTrackingEdit.jsp")})
	public String gotoEditCustomerTracking()throws Exception{
		tList = trackingService.getAllTrackingInfo();
		if(trackingForm !=null && trackingForm.getId() != null){
			CustomerTracking customerTracking = trackingService.getCustomerTrackingById(trackingForm.getId());
			trackingForm.setContent(customerTracking.getContent());
			trackingForm.setCreateDate(customerTracking.getCreateDate());
			trackingForm.setTrackingDate(customerTracking.getTrackingDate());
			trackingForm.setCustomerId(customerTracking.getCustomer().getId().toString());
			trackingForm.setId(customerTracking.getId().toString());
			trackingForm.setTrackingInfoId(customerTracking.getTrackingInfo().getId().toString());
		}
		customerWrapperList=customerService.findAllCustomer(customerCondition);
		return "success";
	}
	
	@Action(value="gotoDeleteCustomerTracking",results={@Result(location="/pages/sale/customerTrackingDel.jsp")})
	public String gotoDeleteCustomerTracking(){
		return "success";
	}
	
	@Action(value="delCustomerTracking")
	public String delCustomerTracking(){
		trackingService.deleteCustomerTracking(trackingForm.getId());
		this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="editCustomerTracking")
	public String editCustomerTracking(){
		CustomerTracking customerTracking = new CustomerTracking();
		if(trackingForm != null && trackingForm.getId() != null && !trackingForm.getId().equals("")){
			customerTracking.setId(trackingForm.getId());
		}else{
			customerTracking.setId(null);
		}
		User user = new User();
		user.setId(getUserMap().get("userId").toString());
		Customer customer = new Customer();
		customer.setId(trackingForm.getCustomerId());
		TrackingInfo trackingInfo = new TrackingInfo();
		trackingInfo.setId(trackingForm.getTrackingInfoId());
		customerTracking.setContent(trackingForm.getContent());
		customerTracking.setCreateDate(trackingForm.getCreateDate());
		customerTracking.setTrackingDate(trackingForm.getTrackingDate());
		customerTracking.setCustomer(customer);
		customerTracking.setTrackingInfo(trackingInfo);
		customerTracking.setCreateDate(DateUtil.getDate());
		customerTracking.setUser(user);
		trackingService.saveCustomerTracking(customerTracking);
		this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="getCustomerTrackingByUid",results={@Result(location="/pages/sale/customerTracking.jsp")})
	public String getCustomerTrackingByUid(){
		String uid = getUserMap().get("userId").toString();
		ctList = trackingService.getCustomerTrackingByUidAndCustomer(uid,trackingForm.getCustomerId());
		return "success";
	}
	public String getAllTrackingInfo(){
		return null;
	}

	public ITrackingService getTrackingService() {
		return trackingService;
	}

	public void setTrackingService(ITrackingService trackingService) {
		this.trackingService = trackingService;
	}

	public List<CustomerTracking> getCtList() {
		return ctList;
	}

	public void setCtList(List<CustomerTracking> ctList) {
		this.ctList = ctList;
	}

	public CustomerTrackingForm getTrackingForm() {
		return trackingForm;
	}

	public void setTrackingForm(CustomerTrackingForm trackingForm) {
		this.trackingForm = trackingForm;
	}

	public List<TrackingInfo> gettList() {
		return tList;
	}

	public void settList(List<TrackingInfo> tList) {
		this.tList = tList;
	}

	public ICustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public List<CustomerWrapper> getCustomerWrapperList() {
		return customerWrapperList;
	}

	public void setCustomerWrapperList(List<CustomerWrapper> customerWrapperList) {
		this.customerWrapperList = customerWrapperList;
	}

	public CustomerConditionForm getCustomerCondition() {
		return customerCondition;
	}

	public void setCustomerCondition(CustomerConditionForm customerCondition) {
		this.customerCondition = customerCondition;
	}
	
}
